package model

import (
	"aquaman/app/infra/ecode"
	"context"
	"github.com/gocraft/dbr/v2"
	"github.com/pkg/errors"
)



type LotteryType int64

const (
	SSQ LotteryType = 0
	DLT LotteryType = 1
)


type Lottery struct {
	Code 		int64	`db:"code"`
	Date 		string	`db:"date"`
	Red			string	`db:"red"`
	Blue		string	`db:"blue"`
	Sales		float64 `db:"sales"`
	PoolMoney	float64 `db:"pool_money"`
	FirstCount	int64 	`db:"first_count"`
	FirstMoney	float64 `db:"first_money"`
	SecondCount int64 	`db:"second_count"`
	SecondMoney	float64 `db:"second_money"`
	ThirdCount	int64 	`db:"third_count"`
	ThirdMoney	float64 `db:"third_money"`
	Content 	string  `db:"content"`
}


func (repo *Repository) GetLastestLotteryByType(lotteryType LotteryType) *Lottery{
	var table string
	if lotteryType == SSQ {
		table = "ssq"
	}else if lotteryType == DLT{
		table = "dlt"
	}else {
		panic(errors.Wrap(ecode.LotteryTypeNotExist, "lottery type is not exist"))
	}
	var lottery Lottery
	session := repo.db.NewSession(nil)
	_, err := session.Select("*").
		From(table).
		OrderDesc("code").
		Limit(1).
		Load(&lottery)
	if err != nil {
		panic(errors.Wrap(ecode.LotteryQueryError,err.Error()))
	}
	return &lottery
}

func (repo *Repository) GetLotteryByType(lotteryType LotteryType,page uint64, perPage uint64) (list []Lottery, isEnd bool) {

	var table string
	if lotteryType == SSQ {
		table = "ssq"
	}else if lotteryType == DLT{
		table = "dlt"
	}else {
		panic(errors.Wrap(ecode.LotteryTypeNotExist, "lottery type is not exist"))
	}
	session := repo.db.NewSession(nil)
	_, err := session.Select("*").
		From(table).
		OrderDesc("code").
		Paginate(page,perPage).
		Load(&list)
	if err != nil {
		panic(errors.Wrap(ecode.LotteryQueryError,err.Error()))
	}
	var cnt uint64
	_, err = session.Select("count(*)").
		From(table).
		Load(&cnt)
	if err != nil {
		panic(errors.Wrapf(ecode.LotteryQueryError, err.Error()))
	}
	if cnt <= perPage*page {
		isEnd = true
	}

	return
}

func (repo *Repository) UpdateLottery(ctx context.Context,lotteryType LotteryType,model Lottery) {

	tx := repo.db.Tx(ctx)
	var err error
	if lotteryType == SSQ {
		_, err = tx.Update("ssq").
			Where(dbr.Eq("code", model.Code)).
			Set("date",model.Date).
			Set("red",model.Red).
			Set("blue",model.Blue).
			Set("sales",model.Sales).
			Set("pool_money",model.PoolMoney).
			Set("first_count",model.FirstCount).
			Set("first_money",model.FirstMoney).
			Set("second_count",model.SecondCount).
			Set("second_money",model.SecondMoney).
			Set("third_count",model.ThirdCount).
			Set("third_money",model.ThirdMoney).
			Set("content",model.Content).
			Exec()
	}else if lotteryType == DLT{
		_, err = tx.Update("dlt").
			Where(dbr.Eq("code", model.Code)).
			Set("date",model.Date).
			Set("red",model.Red).
			Set("blue",model.Blue).
			Set("pool_money",model.PoolMoney).
			Set("content",model.Content).
			Exec()
	}else {
		panic(errors.Wrap(ecode.LotteryTypeNotExist, "lottery type is not exist"))
	}

	if err != nil {
		panic(errors.Wrap(ecode.LotteryUpdateError,err.Error()))
	}
}

func (repo *Repository) AddLottery(ctx context.Context,lotteryType LotteryType,model Lottery) int64 {
	var table string
	if lotteryType == SSQ {
		table = "ssq"
	}else if lotteryType == DLT{
		table = "dlt"
	}else {
		panic(errors.Wrap(ecode.LotteryTypeNotExist, "lottery type is not exist"))
	}
	tx := repo.db.Tx(ctx)
	r, err := tx.InsertInto(table).
		Columns("code","date",
						"red","blue",
						"sales","pool_money",
						"first_count","first_money",
						"second_count","second_money",
						"third_count","third_money","content").
		Record(&model).
		Exec()
	if err != nil {
		panic(errors.Wrap(ecode.LotteryInsertError,err.Error()))
	}
	n, _ := r.LastInsertId()
	return n
}

func (repo *Repository) CheckLotteryCode(lotteryType LotteryType,code int64) int64  {
	var cnt int64
	var table string
	if lotteryType == SSQ {
		table = "ssq"
	}else if lotteryType == DLT{
		table = "dlt"
	}else {
		panic(ecode.Error(ecode.LotteryTypeNotExist, "lottery type is not exist"))
	}
	session := repo.db.NewSession(nil)
	_, err := session.Select("count(*)").
		From(table).
		Where(dbr.Eq("code", code)).
		Limit(1).
		Load(&cnt)
	if err != nil {
		panic(errors.Wrapf(ecode.LotteryQueryError, "code: %s.", code))
	}

	return cnt
}
